package com.emex.app.web.service;

import com.emex.app.shared.data.Cros;
import com.emex.app.shared.data.CrosExample;
import com.emex.app.shared.data.Position;
import com.emex.app.shared.data.PositionExample;
import com.emex.app.shared.persistance.CrosMapper;
import com.emex.app.shared.persistance.PositionMapper;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Sergey Karavaev
 */
@Service
@Transactional
public class ShopService {

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private CrosMapper crosMapper;

    public int deletePosition(Integer id) {
        return positionMapper.deleteByPrimaryKey(id);
    }

    public Position insertPosition(Position record) {
        positionMapper.insert(record);
        return record;
    }

    public List<Position> selectPositionByExample(PositionExample example) {
        return positionMapper.selectByExample(example);
    }

    public Position getPositionById(Integer id) {
        return positionMapper.selectByPrimaryKey(id);
    }

    public Position updatePosition(Position record) {
        positionMapper.updateByPrimaryKey(record);
        return record;
    }

    public int deleteCros(Integer id) {
        return crosMapper.deleteByPrimaryKey(id);
    }

    public Cros insertCros(Cros record) {
        crosMapper.insert(record);
        return record;
    }

    public List<Cros> selectCrosByExample(CrosExample example) {
        return crosMapper.selectByExample(example);
    }

    public Cros getCrosById(Integer id) {
        return crosMapper.selectByPrimaryKey(id);
    }

    public Cros updateCros(Cros record) {
        crosMapper.updateByPrimaryKey(record);
        return record;
    }

    public List<Position> getPositionByCode(String code) {
        PositionExample positionExample = new PositionExample();
        positionExample.createCriteria().andCodeEqualTo(code);
        List<Position> positions = selectPositionByExample(positionExample);
        return positions;
    }

    public List<Position> getCrossPositionsByPositionId(Integer id) {
        List<Position> analogs = new ArrayList<>();
        CrosExample crosExample = new CrosExample();
        crosExample.createCriteria().andMainIdEqualTo(id);
        List<Cros> crosses = selectCrosByExample(crosExample);
        if (null != crosses && !crosses.isEmpty()) {
            for (Cros cros : crosses) {
                Position position = getPositionById(cros.getAnalogId());
                analogs.add(position);
            }
        }

        CrosExample crosEx = new CrosExample();
        crosEx.createCriteria().andAnalogIdEqualTo(id);
        List<Cros> croses = selectCrosByExample(crosEx);
        if (null != croses && !croses.isEmpty()) {
            for (Cros cros : croses) {
                Position position = getPositionById(cros.getMainId());
                analogs.add(position);
            }
        }
        return analogs;
    }

    public List<Position> getAllPositions() {
        return selectPositionByExample(null);
    }

    public void deleteCrosByPositions(Position p1, Position p2) {
        CrosExample crosExample = new CrosExample();
        crosExample.createCriteria().andMainIdEqualTo(p1.getId()).andAnalogIdEqualTo(p2.getId());
        List<Cros> crosses = selectCrosByExample(crosExample);
        if (null != crosses && !crosses.isEmpty()) {
            for (Cros cros : crosses) {
                deleteCros(cros.getId());
            }
        }

        crosExample = new CrosExample();
        crosExample.createCriteria().andMainIdEqualTo(p2.getId()).andAnalogIdEqualTo(p1.getId());
        List<Cros> croses = selectCrosByExample(crosExample);
        if (null != croses && !croses.isEmpty()) {
            for (Cros cros : croses) {
                deleteCros(cros.getId());
            }
        }
    }
    
    public List<Position> getPositionsToBuy() {
        List<Position> allPositions = selectPositionByExample(null);
        Iterator<Position> iter = allPositions.iterator();
        while (iter.hasNext()) {
            Position pos = iter.next();
            if (pos.getAmount() >= pos.getMinAmount()) {
                iter.remove();
            }
        }
        return allPositions;
    }
}
